package cn.zmwh.manage.admin.modules.business.controller;


import cn.hutool.core.collection.CollUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.constants.Constants;
import cn.zmwh.manage.admin.modules.business.pojo.dto.PropertyValDto;
import cn.zmwh.manage.admin.modules.business.pojo.entity.PropertyVal;
import cn.zmwh.manage.admin.modules.business.service.PropertyValService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dmzmwh
 * @since 2025-03-31
 */
@RestController
@RequestMapping("/api/business/property/val")
public class PropertyValController {

    @Autowired
    private PropertyValService propertyValService;


    @PostMapping("/save")
    @ApiOperation(value = "创建属性")
    public Result save(@RequestBody PropertyValDto request) {
        Set<String> propValues = request.getPropValues();
        String propName = request.getPropName();
        Long uid = request.getUid();
        propertyValService.remove(Wrappers.<PropertyVal>lambdaQuery()
                .eq(PropertyVal::getUid, uid)
                .eq(PropertyVal::getPropName, propName));
        List<PropertyVal> list = new ArrayList<>();
        for (String propValue : propValues) {
            PropertyVal propertyVal = new PropertyVal();
            propertyVal.setPropName(propName);
            propertyVal.setPropValue(propValue);
            propertyVal.setUid(request.getUid());
            propertyVal.setCreateTime(new Date());
            list.add(propertyVal);
        }
        propertyValService.saveBatch(list);
        return Result.success();
    }


    @PostMapping("/del/{id}")
    @ApiOperation(value = "删除属性")
    public Result del(@PathVariable Long id) {
        propertyValService.removeById(id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询属性")
    public Result<List<PropertyValDto>> list(@ApiIgnore @RequestHeader(Constants.UID) Long uid) {
        List<PropertyVal> list = propertyValService.list(Wrappers.<PropertyVal>lambdaQuery()
                .eq(PropertyVal::getUid, uid));
        List<PropertyValDto> reList = new ArrayList<>();
        if (CollUtil.isEmpty(list)) {
            return Result.ok(reList);
        }
        Map<String, List<PropertyVal>> collect = list.stream().collect(Collectors.groupingBy(PropertyVal::getPropName));
        for (Map.Entry<String, List<PropertyVal>> stringListEntry : collect.entrySet()) {
            PropertyValDto propertyValDto = new PropertyValDto();
            propertyValDto.setPropName(stringListEntry.getKey());
            propertyValDto.setPropValues(stringListEntry.getValue().stream().map(PropertyVal::getPropValue).collect(Collectors.toSet()));
            reList.add(propertyValDto);
        }
        return Result.ok(reList);
    }


}

